package com.chb.demo.controller;

import com.chb.demo.dao.ArtistDao;
import com.chb.demo.dao.CompanyDao;
import com.chb.demo.dao.StyleDao;
import com.chb.demo.entity.Admin;
import com.chb.demo.entity.Artist;
import com.chb.demo.entity.Company;
import com.chb.demo.service.AdminService;
import com.chb.demo.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CompanyController {
    @Autowired
    CompanyService companyService;
    @Autowired
    ArtistDao artistDao;
    @Autowired
    CompanyDao companyDao;
    @Autowired
    StyleDao styleDao;

    @RequestMapping("/getcompanys")
    @ResponseBody  // 把返回的结果json序列化
    public Object getCompanys(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize) {
        Page<Company> page = companyService.findAllCompany(pageNo, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "ok");
        map.put("count", page.getTotalElements());
        map.put("data", page.getContent());
        return map;//hello.html..
    }

    @RequestMapping("/getallcompanyartist")
    @ResponseBody  // 把返回的结果json序列化
    public Object getAllCompanyArtists() {
        List<Artist> page = artistDao.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "ok");
        map.put("data", page);
        return map;//hello.html..
    }

    @RequestMapping("/getcompanyartist/{companyname}/{style}/{masterpiece}")
    @ResponseBody  // 把返回的结果json序列化
    public Object getCompanys(@PathVariable("companyname") String companyname
            , @PathVariable("style") String style
            , @PathVariable("masterpiece") String masterpiece) {
        System.out.println(companyname);
        List<Artist> page = companyService.findCompanyArtist(companyname, style, masterpiece);
        //List<Artist> page = artistDao.findAll();

        /*if (companyname != null){
            Integer cid = companyDao.findByDisplayname(companyname).getCid();
            for (Artist a:
                 page) {
                if (a.getCid()!=cid)
                    page.remove(a);
            }
            if (style!=null){
                Integer sid = styleDao.findByDescription(style).getSid();
                for (Artist a:
                        page) {
                    if (a.getSid()!=sid)
                        page.remove(a);
                }
                if (masterpiece!=null){
                    for (Artist a:
                            page) {
                        if (a.getMasterpiece()!=masterpiece)
                            page.remove(a);
                    }
                }
            }
        }*/
        System.out.println(page.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "ok");
        map.put("data", page);
        return map;//hello.html..
    }

    }

